package tjs.study.notes.dotest.proxy.service;


import java.lang.reflect.Method;

public class UserService implements IUserService{

    private String userName = "测试编译时生成getUserName";

    static {
        System.out.println("UserService.class 加载前执行。。。");
    }
    
    public UserService(String userName) {
        this.userName = userName;
    }
    
    

    @Override
    public void login() {
        System.out.println("登陆中。。。");
    }
    
    public final void loginFinal() {
        System.out.println("登陆中 final。。。");
    }

    public static final void logout() {
        System.out.println("登出中。。。");
    }


    public static void sleep() {
        try {
            Thread.currentThread().sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void invokeGetUserName() {
        int successfulTimes = 0;
        while (successfulTimes < 5) {
            try {
                // 加载静态代码块
                //Class<?> classUser = Class.forName(UserService.class.getName(), false, Thread.currentThread().getContextClassLoader());
                Class<?> classUser = Class.forName(UserService.class.getName(), true, Thread.currentThread().getContextClassLoader());
                Method method = classUser.getDeclaredMethod("getUserName");
                System.out.println(method.invoke(this));
                successfulTimes++;
            } catch (Exception ex) {
                System.out.println("NoSuchMethodException，继续运行。。。");
            }
            sleep();
        }
    }
}